package io.bdmc.modules.sys.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.bdmc.common.annotation.SysLogger;
import io.bdmc.common.utils.MyMap;
import io.bdmc.common.utils.MyPage;
import io.bdmc.common.utils.R;
import io.bdmc.common.validator.ValidatorUtils;
import io.bdmc.modules.sys.entity.CascaderData;
import io.bdmc.modules.sys.entity.SysRole;
import io.bdmc.modules.sys.service.SysRoleMenuService;
import io.bdmc.modules.sys.service.SysRoleService;

/**
 * 角色管理
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年11月8日 下午2:18:33
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	
	/**
	 * 角色列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:role:list")
	public R list(@RequestParam Map<String, String> params){
		Long current =Long.valueOf(params.get("current"));
		Long size = Long.valueOf(params.get("size"));
		MyPage<SysRole> myPage = new MyPage<SysRole>(current,size);
        MyPage<SysRole> map = sysRoleService.queryList(myPage);
		return R.ok().putData(MyMap.ok(map.getRecords(), map.getTotal()));
	}
	
	/**
	 * 角色列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:role:select")
	public R select(){
		MyPage<SysRole> myPage = new MyPage<SysRole>(0, 99999);
		MyPage<SysRole> result = sysRoleService.queryList(myPage);
		List<SysRole> list = result.getRecords();
		List<CascaderData> roles = new ArrayList<>();
		for (SysRole role : list) {
			CascaderData d = new CascaderData();
			d.setLabel(role.getRoleName());
			d.setValue(role.getRoleId());
			roles.add(d);
		}
		return R.ok().putData(roles);
	}

	
	
	/**
	 * 角色信息
	 */
	@RequestMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public R info(@PathVariable("roleId") Long roleId){
		SysRole role = sysRoleService.queryObject(roleId);
		
		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		return R.ok().putData(role);
	}
	
	/**
	 * 保存角色
	 */
	@SysLogger("保存角色")
	@RequestMapping("/save")
	@RequiresPermissions("sys:role:save")
	public R save(@RequestBody SysRole role){
		role.setDeptId(getDeptId());
		role.setCreate_by(getUser().getUsername());
		ValidatorUtils.validateEntity(role);
		sysRoleService.saveWithMenu(role);
		return R.ok();
	}
	
	/**
	 * 修改角色
	 */
	@SysLogger("修改角色")
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	public R update(@RequestBody SysRole role){
		role.setDeptId(getDeptId());
		ValidatorUtils.validateEntity(role);
		sysRoleService.updateWithMenu(role);		
		return R.ok();
	}
	
	/**
	 * 删除角色
	 */
	@SysLogger("删除角色")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public R delete(@RequestBody Long roleId){
		sysRoleService.deleteWithMenu(roleId);
		
		return R.ok();
	}
}
